package com.tinycloud.miracll.product.service.impl;
import java.math.BigDecimal;
import com.google.common.collect.Maps;

import cn.hutool.core.date.DateUtil;
import com.tinycloud.common.core.utils.DateUtils;
import com.tinycloud.common.datasource.annotation.Slave4;
import com.tinycloud.miracll.product.domain.*;
import com.tinycloud.miracll.product.mapper.AuxiSysWeightBeginMapper;
import com.tinycloud.miracll.product.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 自动加助剂称量开始数据Service业务层处理
 * 
 * @author tiny
 * @date 2022-05-13
 */
@Service
@Slave4
public class AuxiSysWeightServiceImpl implements IAuxiSysWeightService {

    @Autowired
    IAuxiSysWeightEndService auxiSysWeightEndService;
    @Autowired
    IAuxiSysWeightBeginService auxiSysWeightBeginService;
    @Autowired
    IProductAuxisysWeighMainService productAuxisysWeighMainService;
    @Autowired
    IProductAuxisysWeighDetailService productAuxisysWeighDetailService;

    @Autowired
    IErpProductProdOrderService erpProductProdOrderService;

    @Autowired
    IAuxiSysWeightPointConfService auxiSysWeightPointConfService;

    @Autowired
    IProductInstructionMainService productInstructionMainService;
    @Autowired
    IProductInstructionDetailService productInstructionDetailService;

    @Override
    public void getEndWeightDate() throws ParseException {

        Date endDate = productAuxisysWeighMainService.getMaxWeighEndDate();
        if (null==endDate) {
            endDate = DateUtils.parseDate("2022-05-17",DateUtils.YYYY_MM_DD);
        }

        AuxiSysWeightEnd auxiSysWeightEnd = new AuxiSysWeightEnd();
        auxiSysWeightEnd.setLogtime(endDate);
        List<AuxiSysWeightEnd>  auxiSysWeightEndList = auxiSysWeightEndService.selectAuxiSysWeightEndListByDate(auxiSysWeightEnd);
        System.out.println(auxiSysWeightEndList.size());

        for (AuxiSysWeightEnd auxiSysWeightEndTem : auxiSysWeightEndList ) {
            auxiSysWeightEndTem = auxiSysWeightEndService.selectAuxiSysWeightEndList(auxiSysWeightEndTem).get(0);
            String data16 = auxiSysWeightEndTem.getData16();
            String data18 = auxiSysWeightEndTem.getData18();
            String batchNumReact = data16;
            String batchNumAlcohol = data18;
            AuxiSysWeightBegin auxiSysWeightBegin = new AuxiSysWeightBegin();
            auxiSysWeightBegin.setData16(batchNumReact);


            List<AuxiSysWeightBegin> auxiSysWeightBeginList =  auxiSysWeightBeginService.selectAuxiSysWeightBeginList(auxiSysWeightBegin);
            if (null!=auxiSysWeightBeginList&&0!=auxiSysWeightBeginList.size()) {
                auxiSysWeightBegin = auxiSysWeightBeginList.get(0);
            }else{
                continue;
            }


            ProductInstructionMain productInstructionMainReact  = new ProductInstructionMain();
            ProductInstructionMain productInstructionMainAlcohol  = new ProductInstructionMain();
            productInstructionMainReact.setBatchNum(batchNumReact);
            productInstructionMainAlcohol.setBatchNum(batchNumAlcohol);

            List<ProductInstructionMain> productInstructionMainListReact = productInstructionMainService.selectProductInstructionMainList(productInstructionMainReact);
            List<ProductInstructionMain> productInstructionMainListAlcohol = productInstructionMainService.selectProductInstructionMainList(productInstructionMainAlcohol);
            if (0==productInstructionMainListReact.size()){
                continue;
            }else {
                productInstructionMainReact = productInstructionMainListReact.get(0);
            }

            if(0==productInstructionMainListAlcohol.size()) {
                productInstructionMainAlcohol.setKettlePosit("");
            }else {
                productInstructionMainAlcohol = productInstructionMainListAlcohol.get(0);
            }


            String beginDateStringAct = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,auxiSysWeightBegin.getLogdate());
            String beginTimeStringAct = DateUtils.parseDateToStr(DateUtils.HH_MM_SS,auxiSysWeightBegin.getLogtime());
            String endDateStringAct = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,auxiSysWeightEndTem.getLogdate());
            String endTimeStringAct = DateUtils.parseDateToStr(DateUtils.HH_MM_SS,auxiSysWeightEndTem.getLogtime());

            Date beginDateAct = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,beginDateStringAct+" "+beginTimeStringAct);
            Date endDateAct = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,endDateStringAct+" "+endTimeStringAct);
            String orderNum = productInstructionMainReact.getOrderNum();
            String lineCode = productInstructionMainReact.getLineCode();
            String qcNum = productInstructionMainReact.getQcNum();
            String rcid = productInstructionMainReact.getExcelserverrcid();
            String kettlePosition = productInstructionMainAlcohol.getKettlePosit();
            Float elapseTimeFloat = DateUtils.getDatePoorHour(endDateAct,beginDateAct);

            ProductAuxisysWeighMain productAuxisysWeighMain = new ProductAuxisysWeighMain();
            ProductAuxisysWeighDetail productAuxisysWeighDetail = new ProductAuxisysWeighDetail();

            String mainID = UUID.randomUUID().toString();
            productAuxisysWeighMain.setId(mainID);
            productAuxisysWeighMain.setOrderNum(orderNum);
            productAuxisysWeighMain.setLineNum(lineCode);
            productAuxisysWeighMain.setPreparReactorNum(kettlePosition);
            productAuxisysWeighMain.setQaCtrlNum(qcNum);
            productAuxisysWeighMain.setBatchNum(batchNumReact);
            productAuxisysWeighMain.setBeginTime(beginDateAct);
            productAuxisysWeighMain.setEndTime(endDateAct);
            productAuxisysWeighMain.setElapseTime(elapseTimeFloat.doubleValue());
            productAuxisysWeighMain.setDelFlag(0L);
            productAuxisysWeighMain.setCreateBy("Admin");
            productAuxisysWeighMain.setCreateTime(new Date());
            productAuxisysWeighMain.setUpdateBy("Admin");
            productAuxisysWeighMain.setUpdateTime(new Date());
            productAuxisysWeighMainService.insertProductAuxisysWeighMain(productAuxisysWeighMain);

            for (int i=1;i<=20;i++) {
                AuxiSysWeightPointConf auxiSysWeightPointConf = new AuxiSysWeightPointConf();
                auxiSysWeightPointConf.setNum(Long.valueOf(i));
                auxiSysWeightPointConf = auxiSysWeightPointConfService.selectAuxiSysWeightPointConfList(auxiSysWeightPointConf).get(0);
                if (null==auxiSysWeightPointConf.getMaterialCode()){
                    continue;
                }
                Long num = auxiSysWeightPointConf.getNum();
                productAuxisysWeighDetail = this.productAuxisysWeighDetail(auxiSysWeightBegin,auxiSysWeightEndTem,num.intValue());
                if(null==productAuxisysWeighDetail.getBeginAmount()||null==productAuxisysWeighDetail.getEndAmount()) {
                    continue;
                }

                String pointNum = auxiSysWeightPointConf.getPoint();
                String materialCode = auxiSysWeightPointConf.getMaterialCode();
                ProductInstructionDetail productInstructionDetail = new ProductInstructionDetail();
                productInstructionDetail.setExcelserverrcid(rcid);
                productInstructionDetail.setMaterialCode(materialCode);
                List<ProductInstructionDetail>  productInstructionDetailList = productInstructionDetailService.selectProductInstructionDetailList(productInstructionDetail);
                if(0==productInstructionDetailList.size()) {
                    productInstructionDetail.setPlanFeed(BigDecimal.valueOf(0));
                }else {
                    productInstructionDetail = productInstructionDetailList.get(0);
                }

                BigDecimal formulaAmount = productInstructionDetail.getPlanFeed();
                String name = auxiSysWeightPointConf.getName();
                String detailID = UUID.randomUUID().toString();
                productAuxisysWeighDetail.setId(detailID);
                productAuxisysWeighDetail.setPid(mainID);
                productAuxisysWeighDetail.setPointNum(pointNum);
                productAuxisysWeighDetail.setAuxiName(name);
                productAuxisysWeighDetail.setAmount(productAuxisysWeighDetail.getBeginAmount().subtract(productAuxisysWeighDetail.getEndAmount()));
                productAuxisysWeighDetail.setFormulaAmount(formulaAmount);
                productAuxisysWeighDetail.setDeviationAmount(productAuxisysWeighDetail.getAmount().subtract(formulaAmount));
                productAuxisysWeighDetail.setDelFlag(0L);
                productAuxisysWeighDetail.setCreateBy("Admin");
                productAuxisysWeighDetail.setCreateTime(new Date());
                productAuxisysWeighDetail.setUpdateBy("Admin");
                productAuxisysWeighDetail.setUpdateTime(new Date());
                productAuxisysWeighDetailService.insertProductAuxisysWeighDetail(productAuxisysWeighDetail);
            }
        }


    }

    private  ProductAuxisysWeighDetail productAuxisysWeighDetail (AuxiSysWeightBegin auxiSysWeightBegin, AuxiSysWeightEnd auxiSysWeightEnd, int i) {

        ProductAuxisysWeighDetail productAuxisysWeighDetail = new ProductAuxisysWeighDetail();
        BigDecimal beginVale = null;
        BigDecimal endValue =  null;
        try {
            if (i==1) {
                beginVale = new BigDecimal(auxiSysWeightBegin.getData1()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData1()) ;
            }else if(i==2){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData2()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData2()) ;
            }else if(i==3){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData3()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData3()) ;
            }else if(i==4){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData4()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData4()) ;
            }else if(i==5){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData5()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData5()) ;
            }else if(i==6){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData6()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData6()) ;
            }else if(i==7){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData7()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData7()) ;
            }else if(i==8){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData8()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData8()) ;
            }else if(i==9){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData9()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData9()) ;
            }else if(i==10){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData10()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData10()) ;
            }else if(i==11){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData11()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData11()) ;
            }else if(i==12){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData12()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData12()) ;
            }else if(i==13){
                return productAuxisysWeighDetail;
              /*  beginVale = new BigDecimal(auxiSysWeightBegin.getData13()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData13()) ;*/
            }else if(i==14){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData14()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData14()) ;
            }else if(i==15){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData15()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData15()) ;
            }else if(i==16){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData16()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData16()) ;
            }else if(i==17){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData17()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData17()) ;
            }else if(i==18){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData18()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData18()) ;
            }else if(i==19){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData19()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData19()) ;
            }else if(i==20){
                beginVale = new BigDecimal(auxiSysWeightBegin.getData19()) ;
                endValue = new BigDecimal(auxiSysWeightEnd.getData19()) ;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        productAuxisysWeighDetail.setBeginAmount(beginVale);
        productAuxisysWeighDetail.setEndAmount(endValue);

        return productAuxisysWeighDetail;
    }



}
